package com.yaxin.lucene;

import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.hadoop.hdfs.lucene.po.NioResult;

public class ClientHandler implements IoHandler{
	private String _class;
	private String callback;
	private CallBack call;
	
	public ClientHandler(){
		
	}
	
	@Deprecated
	public ClientHandler(String _class,String callback){
		this._class = _class;
		this.callback = callback;
	}
	
	public ClientHandler(CallBack call){
		this.call = call;
	}

	public void exceptionCaught(IoSession arg0, Throwable arg1)
			throws Exception {
		arg1.printStackTrace();
	}

	public void messageReceived(IoSession session, Object arg1) throws Exception {
		System.out.println("client received");
		if(arg1 instanceof byte[]){
			NioResult rs = (NioResult) ByteUtil.byteToObject((byte[])arg1);
			call.doCall(rs);
			session.close(true);
		}
	}

	public void messageSent(IoSession arg0, Object arg1) throws Exception {
		
	}

	public void sessionClosed(IoSession arg0) throws Exception {
		
	}

	public void sessionCreated(IoSession arg0) throws Exception {
		
	}

	public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
		
	}

	public void sessionOpened(IoSession arg0) throws Exception {
		
	}

}
